﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.Objects;

namespace ArkanoidX.UI
{
    public class Menu : Window
    {
        public List<Text> Items = new List<Text>();
        public SpriteFont NormalFont;
        public SpriteFont HoveredFont;
        public SpriteFont SelectedFont;

        public Rectangle BoundingBox;

        public int SelectedIndex = -1;

        protected bool _dashing = false;
        protected TimeSpan _pressDuration;
        protected TimeSpan _dashThreshold = new TimeSpan(0, 0, 0, 0, 500);

        public delegate void ItemSelectedDelegate(Menu menu, int index);

        public event ItemSelectedDelegate ItemSelected;

        public Menu(MainGame game)
            : base(game)
        {
            canBeActivated = true;
            canBeFocused = true;
        }

        public void SetBoundingBox(Rectangle boundingBox)
        {
            BoundingBox = boundingBox;
        }

        public void AddItems(String[] values)
        {
            int count = values.Length;
            float itemHeight = BoundingBox.Height / count;
            int currTop;
            int currBottom = BoundingBox.Top;
            for (int i = 0; i < count; i++)
            {
                String value = values[i];
                Text text = new Text((MainGame)Game);
                text.Value = value;

                currTop = currBottom;
                currBottom = (int)Math.Round(BoundingBox.Top + itemHeight * (i + 1));

                Rectangle itemRect = new Rectangle(BoundingBox.Left, 
                    currTop, BoundingBox.Width, currBottom - currBottom);
                text.Align(itemRect, Window.AlignType.Center, 0, 0);
                Items.Add(text);
            }
        }

        public override void TryHandleKeyEvent(GameTime gameTime, Window.KeyStrokeEvent e)
        {
            if (e.key == Keys.Up)
            {
                if (!((MainGame)Game).lkbsUI.LastUpPressed) _dashing = false;

                if (((MainGame)Game).lkbsUI.LastUpPressed && _pressDuration < _dashThreshold && !_dashing)
                {
                    _pressDuration += gameTime.ElapsedGameTime;
                    if (_pressDuration >= _dashThreshold)
                    {
                        _pressDuration = TimeSpan.Zero;
                        _dashing = true;
                    }
                }
                else
                {
                    SelectedIndex--;
                    if (SelectedIndex < 0)
                        SelectedIndex = 0;
                    _pressDuration = gameTime.ElapsedGameTime;
                }
                e.consumed = true;
            }
            else if (e.key == Keys.Down)
            {
                if (!((MainGame)Game).lkbsUI.LastDownPressed) _dashing = false;
                if (((MainGame)Game).lkbsUI.LastDownPressed && _pressDuration < _dashThreshold && !_dashing)
                {
                    _pressDuration += gameTime.ElapsedGameTime;
                    if (_pressDuration >= _dashThreshold)
                    {
                        _pressDuration = TimeSpan.Zero;
                        _dashing = true;
                    }
                }
                else
                {
                    SelectedIndex++;
                    if (SelectedIndex >= Items.Count)
                        SelectedIndex = Items.Count - 1;
                    _pressDuration = gameTime.ElapsedGameTime;
                }
                e.consumed = true;
            }
            else
            {
                if (e.key == Keys.Enter)
                {
                    ItemSelected(this, SelectedIndex);
                }
                _pressDuration = gameTime.ElapsedGameTime;
                e.consumed = true;
            }

            base.TryHandleKeyEvent(gameTime, e);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Text text = Items[i];
                if (i == SelectedIndex)
                {
                    text.Color = Color.Red;
                }
                else
                {
                    text.Color = Color.White;
                }
                text.Draw(gameTime);
            }

            base.Draw(gameTime);
        }

    }
}
